import numpy as np
import matplotlib.pyplot as plt
import cv2

img = cv2.imread('./img/sector1.tif')
print('img info:')
print('shape:',img.shape,' pixal num:',img.size,' dtype:',img.dtype)
zeros = np.zeros(img.shape[:2], dtype = "uint8")
zeros = np.zeros(img.shape[:2], dtype = "uint8")
#不推荐
#img[10][10]=[0,0,0]
#print(img[10][10])  
#矩阵方法，推荐
# 获取像素值
#print (img.item(10,10,0),img.item(10,10,1),img.item(10,10,2))
# 修改像素值
#img.itemset((10,10,2),255)
#print (img.item(10,10,2))
#鼠标回调函数 
#事件 坐标 标志 返回
def show_num(event,y,x,flags,param):
    if event==cv2.EVENT_LBUTTONUP:
        print('POS:(',x,y,')' ,img_res[x][y])
        cv2.circle(img_res,(x,y),10,(255,255,255),-1)
        
    elif event==cv2.EVENT_MBUTTONDOWN:
        print(num)

def range_the_value(img,threahold,color_set):
    width=img.shape[0]
    height=img.shape[1]
    print('>>>',width,height)
    img_b = np.zeros(img.shape[:2], dtype = "uint8")
    img_g = np.zeros(img.shape[:2], dtype = "uint8")
    img_r = np.zeros(img.shape[:2], dtype = "uint8")
    img_rgb=cv2.merge([img_b,img_g,img_r])

    level_1=(img>threahold[0])*(img<threahold[1])
    level_2=(img>threahold[1])*(img<threahold[2])
    level_3=(img>threahold[2])*(img<threahold[3])
    level_4=(img>threahold[3])*(img<threahold[4])
    level_5=(img>threahold[4])
    #level_1=level_1*img
    
    img_rgb+=cv2.merge([level_5*(img_b+color_set[0][0]),level_5*(img_g+color_set[0][1]),level_5*(img_r+color_set[0][2])])
    img_rgb+=cv2.merge([level_4*(img_b+color_set[1][0]),level_4*(img_g+color_set[1][1]),level_4*(img_r+color_set[1][2])])
    img_rgb+=cv2.merge([level_3*(img_b+color_set[2][0]),level_3*(img_g+color_set[2][1]),level_3*(img_r+color_set[2][2])])
    img_rgb+=cv2.merge([level_2*(img_b+color_set[3][0]),level_2*(img_g+color_set[3][1]),level_2*(img_r+color_set[3][2])])
    img_rgb+=cv2.merge([level_1*(img_b+color_set[4][0]),level_1*(img_g+color_set[4][1]),level_1*(img_r+color_set[4][2])])


    sum=np.sum(level_1)+np.sum(level_2)+np.sum(level_3)+np.sum(level_4)+np.sum(level_5)
    '''
    print("level_1:",np.sum(level_1)/sum)
    print("level_2:",np.sum(level_2)/sum)
    print("level_3:",np.sum(level_3)/sum)
    print("level_4:",np.sum(level_4)/sum)
    print("level_5:",np.sum(level_5)/sum)
    '''
    ratio=[]
    ratio.append(round(np.sum(level_1)/sum*100,3))
    ratio.append(round(np.sum(level_2)/sum*100,3))
    ratio.append(round(np.sum(level_3)/sum*100,3))
    ratio.append(round(np.sum(level_4)/sum*100,3))
    ratio.append(round(np.sum(level_5)/sum*100,3))

    return img_rgb,ratio
#提取图片部分ROI
#img1=img[100:100,200:200]
#img[200:200,300:300]=img1
#RGB拆分
b,g,r=cv2.split(img)

#RGB组合br
#img2=cv2.merge([b,g,r])
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
#cv2.namedWindow('image_test',cv2.WINDOW_NORMAL)
cv2.namedWindow('image_res',cv2.WINDOW_NORMAL)
#cv2.imshow('image',img)
#cv2.imshow('image',g)
#img_res=(1-(r/g))*255
CCD=(r/(g+1.0))
LAI=(g.astype(float)-r.astype(float))
GMMI=CCD*LAI
img_res=GMMI

#ratio=(255/(np.max(img_res)-np.min(img_res)))
#img_res= (img_res-np.min(img_res))*ratio
print('='*40)
print('shape:',img_res.shape,' pixal num:',img_res.size,' dtype:',img_res.dtype)
print('max:',np.max(img_res),' pos: ',np.where(img_res==np.max(img_res)))
print('min:',np.min(img_res),' pos: ',np.where(img_res==np.min(img_res)))
print('='*40)
img_res = np.clip(img_res, 0, 255)# 归一化也行
img_res = np.array(img_res,np.uint8)
img_res=cv2.equalizeHist(img_res)
cv2.imshow('image',img_res)
#cv2.imshow('image',cv2.merge([b, zeros, zeros]))#带颜色的单通道灰度图
#cv2.imshow('image',cv2.merge([zeros, g, zeros]))#带颜色的单通道灰度图
#cv2.imshow('image',cv2.merge([zeros, zeros, r]))#带颜色的单通道灰度图
cv2.setMouseCallback('image_res',show_num)
#逐渐变绿
color_set=[[0,56,255],
            [0,100,255],
            [0,255,255],
            [0,255,150],
            [0,255,40]]
#逐渐变绿
threa_hold=np.array([-59.5,6.8,30.2,50.3,60.1,62.7])
ratio=(255/(np.max(threa_hold)-np.min(threa_hold)))
threa_hold= (threa_hold-np.min(threa_hold))*ratio
#print(threa_hold)
img_test2,growth_ratio=range_the_value(img_res,threa_hold,color_set)
print("Ratio:")

print("level_1:",growth_ratio[0],'%')
print("level_2:",growth_ratio[1],'%')
print("level_3:",growth_ratio[2],'%')
print("level_4:",growth_ratio[3],'%')
print("level_5:",growth_ratio[4],'%')
cv2.imshow('image_res',img_test2)

#cv2.imshow('image_test',cv2.applyColorMap(img_res, cv2.COLORMAP_WINTER))
#print(np.sum(img_res>128))
plt.hist(img_res.ravel(),256,[1,256]);#灰度直方图
plt.show()


while 1:

    #cv2.imshow('image',img_res)
    key=cv2.waitKey(0) 
    if key == 27:
        break
    elif key == ord('s') :# s press
        cv2.imwrite('./img/res.tif',img_res)
        print("save.")
        break
cv2.destroyAllWindows()